package com.randi.web.controller.login;

import com.randi.common.annotation.Anonymous;
import com.randi.framework.domain.dto.UserLoginDTO;
import com.randi.common.core.domain.vo.SaltInfo;
import com.randi.common.core.domain.Result;
import com.randi.framework.domain.service.ISysLoginService;
import com.randi.system.service.ISysSaltService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @author ziye
 */
@RestController
@RequestMapping("")
@Api(value = "sysProfile", tags = "登录管理")
public class SysLoginController {

    @Resource
    private ISysSaltService iSysSaltService;

    @Resource
    private ISysLoginService iSysLoginService;

    /**
     * 用户登录
     *
     * @param userLoginDTO 登录参数
     * @return 结果
     */
    @PostMapping("/login")
    @ApiOperation(value = "用户登录", notes = "")
    public Result login(@RequestBody UserLoginDTO userLoginDTO) {
        HashMap<String, String> token = iSysLoginService.loginByPassword(userLoginDTO);
        return Result.success().put(token);
    }

    /**
     * 登录续期
     *
     * @param request 请求
     * @return Result
     */
    @GetMapping("/token/refresh")
    @ApiOperation(value = "登录续期")
    public Result tokenRefresh(HttpServletRequest request) {
        HashMap<String, String> token = iSysLoginService.refreshToken(request);
        return Result.success().put(token);
    }

    /**
     * 获取加密盐
     *
     * @return 结果
     */
    @Anonymous
    @GetMapping("/salt")
    @ApiOperation(value = "获取加密盐", notes = "")
    public Result getSalt() {
        SaltInfo salt = iSysSaltService.createSaltInfo();
        return Result.success().put(salt);
    }

}
